/*
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.openideals.android.feeder.view;


import java.net.URL;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.openideals.android.feeder.R;
import com.openideals.android.search.MediaSearchService;
import com.openideals.android.search.SearchManager;
import com.openideals.android.search.cruxy.CruxySearch;
import com.openideals.android.search.flickr.FlickrSearch;
import com.openideals.android.search.seeqpod.SeeqpodSearch;
import com.openideals.android.synd.rss.RSSHandler;
import com.openideals.android.synd.xspf.XSPFHandler;


public class URLEditor extends Activity  {

	EditText mText;
    
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        
        setContentView(R.layout.url_editor);

        // Set up click handlers for the text field and button
        mText = (EditText) this.findViewById(R.id.url);
        
        if (icicle != null)
        	mText.setText(icicle.getString("url"));
        
        Button ok = (Button) findViewById(R.id.ok);
        ok.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
            	okClicked();
            }          
        });
        
        Button cancel = (Button) findViewById(R.id.cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
            	finish();
            }          
        });
        
    }

    protected void okClicked() {
    	
    	String feedUrl = mText.getText().toString();
		
    	try {
    		
    		if (feedUrl.indexOf(' ')!=-1)
    		{
    			String searchId = feedUrl.substring(0, feedUrl.indexOf(' '));
    			
    			MediaSearchService mss = SearchManager.getService(searchId);

				String keyword = feedUrl.substring(feedUrl.indexOf(' '+1));
				int start = 0; int max = 10;
				
				String searchUrl = mss.getSearchUrl(keyword, start, max);
				
    			if (mss.getFeedFormat() == MediaSearchService.FORMAT_XSPF)
    			{
    				XSPFHandler.handlePlaylist(this, new URL(searchUrl));
    			}
    			else //must be RSS
    			{
    				RSSHandler rh = new RSSHandler();
    	    		rh.createFeed(this, new URL(searchUrl));
    			}
    		}
    		else if (feedUrl.indexOf("xspf")!=-1)
    		{
    			XSPFHandler.handlePlaylist(this, new URL(feedUrl));
    		}
    		else
    		{
	    		RSSHandler rh = new RSSHandler();
	    		rh.createFeed(this, new URL(feedUrl));
    		}	
	    	
    		finish();
    		
    	} catch (Exception e) {
    		//showAlert("Invalid URL", "The URL you have entered is invalid.", "Ok", false);
    		Log.e("feeder", "unable to parse feed: " + feedUrl + ": " + e);
    	}
    	
    }

    
    /* (non-Javadoc)
	 * @see android.app.Activity#onSaveInstanceState(android.os.Bundle)
	 */
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		
		super.onSaveInstanceState(outState);
		
		outState.putString("url", mText.getText().toString());	
	}

}
